package cn.iocoder.yudao.module.water.service.real;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.water.controller.admin.real.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.real.RealDataDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.water.convert.real.RealDataConvert;
import cn.iocoder.yudao.module.water.dal.mysql.real.RealDataMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.water.enums.ErrorCodeConstants.*;

/**
 * 实时数据 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class RealDataServiceImpl implements RealDataService {

    @Resource
    private RealDataMapper realDataMapper;

    @Override
    @DS("report")
    public Long createRealData(RealDataCreateReqVO createReqVO) {
        // 插入
        RealDataDO realData = RealDataConvert.INSTANCE.convert(createReqVO);
        realDataMapper.insert(realData);
        // 返回
        return realData.getId();
    }

    @Override
    @DS("report")
    public void updateRealData(RealDataUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRealDataExists(updateReqVO.getId());
        // 更新
        RealDataDO updateObj = RealDataConvert.INSTANCE.convert(updateReqVO);
        realDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteRealData(Long id) {
        // 校验存在
        this.validateRealDataExists(id);
        // 删除
        realDataMapper.deleteById(id);
    }

    private void validateRealDataExists(Long id) {
        if (realDataMapper.selectById(id) == null) {
            throw exception(REAL_DATA_NOT_EXISTS);
        }
    }

    @Override
    @DS("report")
    public RealDataDO getRealData(Long id) {
        return realDataMapper.selectById(id);
    }

    @Override
    @DS("report")
    public List<RealDataDO> getRealDataList(Collection<Long> ids) {
        return realDataMapper.selectBatchIds(ids);
    }

    @Override
    @DS("report")
    public PageResult<RealDataDO> getRealDataPage(RealDataPageReqVO pageReqVO) {
        return realDataMapper.selectPage(pageReqVO);
    }

    @Override
    @DS("report")
    public List<RealDataDO> getRealDataList(RealDataExportReqVO exportReqVO) {
        return realDataMapper.selectList(exportReqVO);
    }

    @Override
    @DS("report")
    public List<RealDataDO> getRealDataAllList() {
        return realDataMapper.selectLeastList();
    }

    @Override
    @DS("report")
    public List<RealDataDO> getRealDataList(String tabl, String lable) {
        DateTime dateTime = new DateTime();
        String start = String.valueOf(DateUtil.beginOfDay(dateTime));
        String end = String.valueOf(DateUtil.endOfDay(dateTime));
        return realDataMapper.selectRealDataList(tabl, lable, start, end);
    }
}
